<%@ page contentType="text/html;charset=UTF-8"%>
<%@ include file="/WEB-INF/view/common/easycommon.jsp"%>
<!DOCTYPE html>
<html>
<head>
  <title>明细</title>
  <meta charset="utf-8">
  <script type="text/javascript" src="${basepath}/assets/ztree/jquery.ztree.all-3.5.js"></script>
  <link rel="stylesheet" type="text/css" href="${basepath}/assets/ztree/zTreeStyle.css" />
  <style>
    .btndiv{
      padding-top:10px;
      text-align:center;
      display: inline-block;
      width:100%
    }
  </style>
</head>
<body>
  <form action="" method="post" id="taskTableForm" name="taskTableForm">
    <input type="hidden" id="taskId" name="taskId" value="${taskId}"/>
    <input type="hidden" id="taskUserId" name="taskUserId" value="${taskUserId}"/>
    <table style="width:100%;">
      <c:forEach items="${dataList}" var="tpl">
        <c:choose>
          <c:when test="${tpl.type==0}"><!--普通文本类型-->
            <tr>
              <td style="width:15%;padding:5px;text-align: right">
                <label style="font-size:14px;">${tpl.name}:</label>
              </td>
              <td style="width:85%;padding:5px">
                <input class="easyui-textbox"  name="${tpl.name}" id="${tpl.columnCode}" style="width:300px" value="${tpl.value}">
              </td>
            </tr>
          </c:when>

          <c:when test="${tpl.type==1}"><!--日期类型-->
            <tr>
              <td style="width:15%;padding:5px;text-align: right">
                <label style="font-size:14px;">${tpl.name}:</label>
              </td>
              <td style="width:85%;padding:5px">
                <input class="mytextbox" id="${tpl.columnCode}" name="${tpl.name}" style="width:300px" onfocus="WdatePicker({dateFmt:'yyyy-M-d'})" value="${tpl.value}"/>
              </td>
            </tr>
          </c:when>

          <c:when test="${tpl.type==2}"><!--数字类型-->
            <tr>
              <td style="width:15%;padding:5px;text-align: right">
                <label style="font-size:14px;">${tpl.name}:</label>
              </td>
              <td style="width:85%;padding:5px">
                <input class="easyui-textbox"  name="${tpl.name}" id="${tpl.columnCode}" style="width:300px" value="${tpl.value}">
              </td>
            </tr>
          </c:when>

          <c:when test="${tpl.type==3}"><!--富文本类型-->
            <tr>
              <td style="width:15%;padding:5px;text-align: right">
                <label style="font-size:14px;">${tpl.name}:</label>
              </td>
              <td style="width:85%;padding:5px">
                <textarea class="mytextbox" name=${tpl.name} id="${tpl.columnCode}" style="width:90%;height:360px" >${tpl.value}</textarea>
                <script>
                  var editor_${tpl.columnCode};
                  KindEditor.ready(function(K) {
                    editor_${tpl.columnCode} = K.create('textarea[name="${tpl.name}"]', {
                      items: ["source", "|", "undo", "redo", "|", "preview", "template", "code", "cut", "copy", "paste", "plainpaste", "wordpaste", "|", "justifyleft",
                        "justifycenter", "justifyright", "justifyfull", "insertorderedlist", "insertunorderedlist", "indent", "outdent", "subscript", "superscript",
                        "clearhtml", "quickformat", "selectall", "|", "fullscreen", "/", "formatblock", "fontname", "fontsize", "|", "forecolor", "hilitecolor",
                        "bold", "italic", "underline", "strikethrough", "lineheight", "removeformat", "|", "image", "multiimage", "insertfile", "table",
                        "hr", "emoticons", "baidumap", "pagebreak"],
                      allowFileManager : true,
                      allowImageUpload : true,
                      uploadJson  : '${basepath}/assets/kindedit/jsp/upload_kind.jsp'
                    });
                  });
                </script>
              </td>
            </tr>
          </c:when>
        </c:choose>
      </c:forEach>
    </table>
    <div class="btndiv" >
      <a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-save'" style="width:12%" onclick="formSubmit('cg')">保存草稿</a>
      <a id="searchbth" href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-redo'" onclick="javascript:$('#transWin').window('open')">下发</a>
      <a id="finishbth" href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-ok'" onclick="finishSubmit()">完结/提交</a>
    </div>
  </form>

  <div id="transWin" class="easyui-window" title="下发" style="width:680px;height:480px;padding:10px;"
       data-options="iconCls:'icon-save',modal:true,closed:true">
    <form id="transForm" name="transForm" method="post">
      <table cellpadding="5">
        <tr>
          <td>部门:</td>
          <td>流转备注:</td>
        </tr>
        <tr>
          <td>
            <div style="padding:10px;width:300px;height:300px;overflow-y:auto">
              <ul id="detptree" class="ztree"></ul>
            </div>
          </td>
          <td><input class="easyui-textbox" name="note" data-options="multiline:true"
                     style="height:300px;width:300px;"/></td>
        </tr>
      </table>
      <div style="text-align:right;margin-top:10px;padding-right:33px">
        <a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-save'" style="width:25%"
           onclick="formSubmit('xf')">确定下发</a>
        <a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-clear'" style="width:15%"
           onclick="javascript:$('#transWin').window('close')">关闭</a>
      </div>
    </form>
  </div>

</body>
<script>
  //下发保存
  var formSubmit = function(type){
    var formval = serializeForm(taskTableForm);
    formval = JSON.parse(formval);
    <c:forEach items="${dataList}" var="tpl">
      <c:choose>
        <c:when test="${tpl.type==3}"><!--富文本类型-->
          formval.${tpl.name} = editor_${tpl.columnCode}.html();
        </c:when>
      </c:choose>
    </c:forEach>
    var dataList = new Array();
    dataList.push(formval);
    $.ajax({
      url: '${path}/cootasktable/saveFormData',
      contentType: "application/json",
      type:'POST',
      data:  JSON.stringify(dataList),
      dataType: 'json',
      success: function(doc) {
        if(type=='xf'){
          transSubmit(doc);
        }else if(type=='cg'){
          layer.msg(doc.msg, {time: 3000, icon:6});
        }
        //layer.msg(doc.msg, {time: 3000, icon:6});
      }
    });
  }

  //完结保存
  var finishSubmit = function(){
    $.messager.confirm("提示", "完结后任务将结束流转，是否进行此操作?", function (r) {
      if (r) {
        var formval = serializeForm(taskTableForm);
        formval = JSON.parse(formval);
        <c:forEach items="${dataList}" var="tpl">
        <c:choose>
        <c:when test="${tpl.type==3}"><!--富文本类型-->
        formval.${tpl.name} = editor_${tpl.columnCode}.html();
        </c:when>
        </c:choose>
        </c:forEach>
        var dataList = new Array();
        dataList.push(formval);
        $.ajax({
          url: '${path}/cootasktable/saveFormData',
          contentType: "application/json",
          type:'POST',
          data:  JSON.stringify(dataList),
          dataType: 'json',
          success: function(doc) {
            finish(doc);
          }
        });
      }
    })
  }

  //下发
  function transSubmit(doc){
    if (doc.status=='0') {
      return $.messager.alert("提示", doc.msg, "error");
    }
    var zTree = $.fn.zTree.getZTreeObj("detptree"),
            nodes = zTree.getChangeCheckedNodes(),
            checkedUserIds = [],checkedUserNames = [],
            checkedUserDeptIds = [],checkedUserDeptNames = [];
    for (var i=0, l=nodes.length; i<l; i++) {
      if(nodes[i].datatype=='u'){
        checkedUserIds.push(nodes[i].id);
        checkedUserNames.push(nodes[i].name);
        checkedUserDeptIds.push(nodes[i].deptId);
        checkedUserDeptNames.push(nodes[i].deptName);
      }
    }
    if (checkedUserIds.length==0) {
      return $.messager.alert("提示", "请选择下发人员", "error");
    }
    if ($('#transForm').form('enableValidation').form('validate')) {
      var formval = JsonCode(transForm);
      formval = JSON.parse(formval);
      formval.id = '${taskUserId}';//流转id
      formval.fTaskHeadid = '${taskId}';//任务id
      formval.fromUserId = '${fromUserId}';//本级用户
      formval.fromUserName = '${fromUserName}';
      formval.fromDeptId = '${fromDeptId}';//本级部门
      formval.fromDeptName = '${fromDeptName}';
      formval.toUserId = checkedUserIds.join(",");
      formval.toUserName = checkedUserNames.join(",");
      formval.toDeptId = checkedUserDeptIds.join(",");
      formval.toDeptName = checkedUserDeptNames.join(",");
      if('${transferMaxLevel}'!=''){//如果存在最大次数限制，则最大次数加1
        if(parseInt('${transferLevel}')<parseInt('${transferMaxLevel}')){
          formval.transferLevel = parseInt('${transferLevel}')+1;
        }else{
          return $.messager.alert("提示", "任务流转已达最大层数，请完结任务", "error");
        }
      }
      var actionUrl = "${path}/cootask/saveTaskUser";
      $.ajax({
        type: "POST",
        url: actionUrl,
        data: JSON.stringify(formval),
        dataType: 'json',
        contentType: "application/json;charset=utf-8",
        success: function (text) {
          if (text.status == 1) {
            $('#transWin').window('close');
            parent.closeAll(text);
          }
          layer.msg(text.msg, {time: 3000, icon: 6});
        },
        error: function (jqXHR, textStatus, errorThrown) {
          layer.msg("与远程服务器交互异常", {time: 3000, icon: 6});
        }
      });
    }
  }

  //完结
  var finish = function(doc){
    if (doc.status=='0') {
      return $.messager.alert("提示", doc.msg, "error");
    }
    var formval = {};
    formval.id = '${taskUserId}';//流转id
    formval.fTaskHeadid = '${taskId}';//任务id
    var actionUrl = "${path}/cootask/finishTaskUser";
    $.ajax({
      type: "GET",
      url: actionUrl,
      data: formval,
      dataType: 'json',
      contentType: "application/json;charset=utf-8",
      success: function (text) {
        if (text.status == 1) {
          $('#transWin').window('close');
          parent.closeAll(text);
        }
        layer.msg(text.msg, {time: 3000, icon: 6});
      },
      error: function (jqXHR, textStatus, errorThrown) {
        layer.msg("与远程服务器交互异常", {time: 3000, icon: 6});
      }
    });
  }
  $(document).ready(function(){
    var settingmenu = {
      data: {
        simpleData: {
          enable: true
        }
      },
      check: {
        enable: true,
        nocheckInherit: true
      }
    };

    $.ajax({
      url: "${path}/commonSel/easyTreeDeptUser?dept_id=${deptId}",
      success: function (text) {
        $.fn.zTree.init($("#detptree"), settingmenu, text);
      },
      error: function (jqXHR, textStatus, errorThrown) {
        toastr.info("与远程服务器交互异常，请规范操作或重新登录", "提示", toastrOptsBottom);
      }
    });
  })
</script>
</html>
